package com.hamster.crm.web;

import com.hamster.crm.mapper.UserRoleMapper;
import com.hamster.crm.pojo.SchemeEmployee;
import com.hamster.crm.pojo.User;
import com.hamster.crm.pojo.UserRole;
import com.hamster.crm.pojo.dto.DepartmentUserList;
import com.hamster.crm.pojo.dto.PermissionGroupDTO;
import com.hamster.crm.service.PermissionService;
import com.hamster.crm.service.UserRoleService;
import com.hamster.crm.service.UserService;
import com.hamster.crm.util.PageResult;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Tarry
 * @create 2020/1/2 9:50
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private PermissionService permissionService;
    /**
     * 添加新用户账号密码
     * @param user
     * @return
     */
    @PostMapping("addUser")
    public ResponseEntity<Void> addUser(@RequestBody User user){
        userService.addUser(user);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改用户
     * @param user
     * @return
     */
    @PostMapping("updateUser")
    public ResponseEntity<Void> updateUser(@RequestBody User user){
        userService.updateUser(user);
        return ResponseEntity.ok().build();
    }

    /**
     * 根据id 删除用户
     * @param map id
     * @return
     */
    @PostMapping("deleteUser")
    public ResponseEntity<Void> deleteUser(@RequestBody Map<String,String> map){

        userService.deleteUser(map.get("id"));
        return ResponseEntity.ok().build();
    }

    /**
     * 条件查询 user 列表
     * @param pageSize     每页条数
     * @param currentPage  当前页号
     * @param phone        根据账号模糊查询
     * @return
     */
    @GetMapping("listUsers")
    public ResponseEntity<PageResult<User>> listUsers(
            @RequestParam(required=false) Integer pageSize,
            @RequestParam(required=false) Integer currentPage,
            @RequestParam(required = false)String phone
    ){
        PageResult<User> resultList = userService.listSchemeResults(phone,currentPage,pageSize);
        return ResponseEntity.ok(resultList);
    }


    /**
     * 获取用户下拉列表【角色关联用户使用】
     * @return 用户list
     */
    @GetMapping("listUsersSelect")
    public ResponseEntity<List<DepartmentUserList>> listUsersSelect(){
        List<DepartmentUserList> list = userService.listUsersSelect();
        return ResponseEntity.ok(list);
    }


    /**
     * 新增角色
     * @param userRole
     * @return
     */
    @PostMapping("insertRole")
    public ResponseEntity<Void> insertRole(@RequestBody UserRole userRole){
        userRoleService.insertRole(userRole);
        return ResponseEntity.ok().build();
    }

    /**
     * 获取角色列表
     * @return
     */
    @GetMapping("listRoles")
    public ResponseEntity<List<UserRole>> listRoles(){
        List<UserRole> list = userRoleService.listRoles();
        return ResponseEntity.ok(list);
    }


    /**
     * 根据角色的id，获取该角色下面的 user 列表
     * @param roleId 角色id
     * @return 用户列表
     */
    @GetMapping("listUserByRole")
    public ResponseEntity<List<User>> listUserByRole(@RequestParam String roleId){
        List<User> list = userRoleService.listUserByRole(roleId);
        for (User user : list) {
            user.setRoleId(roleId);
        }
        return ResponseEntity.ok(list);
    }

    /**
     * 为指定的角色添加 user 用户
     * @param roleId 角色id     param
     * @param idList userId集合  body
     * @return
     */
    @PostMapping("insertRoleUser")
    public ResponseEntity<Void> insertRoleUser(@RequestParam String roleId,@RequestBody List<String> idList){

        userRoleService.insertRoleUser(roleId,idList);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除指定角色中的，指定用户
     * @param roleId 角色id
     * @param userId 用户id
     * @return
     */
    @PostMapping("deleteRoleUser")
    public ResponseEntity<Void> deleteRoleUser(@RequestBody Map<String,String> map){
        userRoleService.deleteRoleUser(map.get("roleId"),map.get("userId"));
        return ResponseEntity.ok().build();
    }

    /**
     * 获取全部权限列表
     * @return
     */
    @GetMapping("listPermissions")
    public ResponseEntity< List<PermissionGroupDTO>> listPermissions(){
       List<PermissionGroupDTO> list = permissionService.listPermissions();
       return ResponseEntity.ok(list);
    }

    /**
     * 保存权限
     * @param ids 已选的权限id
     * @return
     */
    @PostMapping("savePermission")
    public ResponseEntity<Void> savePermission(@RequestParam String roleId,@RequestBody List<String> ids){
        permissionService.savePermission(roleId,ids);
        return ResponseEntity.ok().build();
    }

    /**
     * 获取该角色的权限列表
     * @param roleId
     * @return
     */
    @GetMapping("getPermission")
    public ResponseEntity<List<String>> getPermissionByRoleId(@RequestParam String roleId){
        List<String> list = permissionService.getPermissionByRoleId(roleId);
        return ResponseEntity.ok(list);
    }

    /**
     * 删除角色
     * @param roleId
     * @return
     */
    @GetMapping("deleteRole")
    public ResponseEntity<Void> deleteRole(@RequestParam String roleId){
        userRoleService.deleteRole(roleId);

        return ResponseEntity.ok().build();
    }


}
